﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Stats : System.Web.UI.Page
{
    decimal TotalAtlasKbOut = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack) 
        {
            cldrDateStart.SelectedDate = DateTime.Today.AddDays(-1);
            cldrDateStart.VisibleDate = DateTime.Today;
            cldrDateStop.SelectedDate = DateTime.Today.AddDays(1);
            cldrDateStop.VisibleDate = DateTime.Today.AddDays(1);
            cldrDateStart_SelectionChanged(sender, e);
        }
        //TotalAtlasByteOut = 0;
        /*if (Context.Request.QueryString["ViewIndex"] != null)
        {
            mvStats.ActiveViewIndex = Convert.ToByte(Context.Request.QueryString["ViewIndex"]);
        };*/
    }
    protected void ddlTypeQuery_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (ddlTypeQuery.SelectedIndex)
        {
            case 0: cldrDateStop.Visible = false; Label1.Visible = false; gvStatsEP.Columns[5].Visible = true;
                gvStatsEP_SNG.Columns[6].Visible = true; 
                //dsStatsHosts.FilterExpression = "dbegin_stat='{0}'";
                break;
            case 1: cldrDateStop.Visible = true; Label1.Visible = true; gvStatsEP.Columns[5].Visible = false;
                gvStatsEP_SNG.Columns[6].Visible = false; 
                //dsStatsHosts.FilterExpression = "(dbegin_stat >= '{0}') and (dbegin_stat <= '{1}')"; 
                break;
        }
        cldrDateStart_SelectionChanged(sender, e);
    }
    protected void cldrDateStart_SelectionChanged(object sender, EventArgs e)
    {
        lbHeader.ForeColor = System.Drawing.Color.White;
        if (ddlTypeQuery.SelectedIndex == 0)
        {
            //cldrDateStop.SelectedDate = cldrDateStart.SelectedDate.Date.AddDays(1);
            cldrDateStop.SelectedDate = cldrDateStart.SelectedDate.Date.AddDays(0);
            lbHeader.Text = "за указанную дату: " + cldrDateStart.SelectedDate.Date.ToShortDateString();            
        }
        else
        {
            if (cldrDateStart.SelectedDate.Date > cldrDateStop.SelectedDate.Date)
            {
                lbHeader.Text = "Некорректный диапазон дат. Дата начала должна быть меньше даты конца!";
                lbHeader.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                lbHeader.Text = "за период с " + cldrDateStart.SelectedDate.Date.ToShortDateString() + " по " + cldrDateStop.SelectedDate.Date.ToShortDateString();
            }

        }
        TotalAtlasKbOut = 0;
        gvStatsEP.DataBind();
        gvStatsHosts.DataBind();
        gvStatsFR.DataBind();
        lbTotalAtlas.Text = System.String.Format("Итого по сети 'Атлас': {0:n2} Мб.", TotalAtlasKbOut / 1024);
    }
    protected void gvStatsEP_DataBound(object sender, EventArgs e)
    {
        Int64 TotalMsgIn = 0;
        Int64 TotalByteIn = 0;
        Int64 TotalMsgErr = 0;
        Int64 TotalMsgUrg = 0;
        for (int i = 0; i < gvStatsEP.Rows.Count;i++)
            {
                if (gvStatsEP.EditIndex != i)
                {
                    TotalMsgIn = TotalMsgIn + Convert.ToInt64(gvStatsEP.Rows[i].Cells[2].Text);
                    TotalByteIn = TotalByteIn + Convert.ToInt64(gvStatsEP.Rows[i].Cells[5].Text);
                    TotalMsgErr = TotalMsgErr + Convert.ToInt64(gvStatsEP.Rows[i].Cells[4].Text);
                    TotalMsgUrg = TotalMsgUrg + Convert.ToInt64(gvStatsEP.Rows[i].Cells[3].Text);
                }
            }
            TotalAtlasKbOut = TotalAtlasKbOut + TotalByteIn;
            lbStatsEP.Text = System.String.Format("Итого по ЭП Атлас принято сообщений {0:n0} шт. (из них срочных {2:n0}, ошибок {3:n0}), общий объем {1:n2} Мб.",
                TotalMsgIn, TotalByteIn / 1024, TotalMsgUrg, TotalMsgErr);
    }
    protected void gvStatsHosts_DataBound(object sender, EventArgs e)
    {
        int TotalCalls = 0;
        decimal TotalByteIn = 0;
        decimal TotalByteOut = 0;
        for (int i = 0; i < gvStatsHosts.Rows.Count; i++)
        {
            TotalCalls = TotalCalls + Convert.ToInt32(gvStatsHosts.Rows[i].Cells[1].Text);
            TotalByteIn = TotalByteIn + Convert.ToInt32(gvStatsHosts.Rows[i].Cells[3].Text);
            TotalByteOut = TotalByteOut + Convert.ToInt32(gvStatsHosts.Rows[i].Cells[2].Text);
        }
        TotalAtlasKbOut = TotalAtlasKbOut + TotalByteOut + TotalByteIn;
        lbStatsHosts.Text = System.String.Format("Итого по хостам: {0:n0} вызовов, передано {1:n2} Мб. - принято {2:n2} Мб.",
            TotalCalls, TotalByteOut/1024, TotalByteIn/1024);
    }
    protected void gvStatsFR_DataBound(object sender, EventArgs e)
    {
        int TotalFR_Mb = 0;
        for (int i = 0; i < gvStatsFR.Rows.Count; i++)
        {
            if (gvStatsFR.Rows[i].Cells[2].Text != "")
            {
                TotalFR_Mb = TotalFR_Mb + Convert.ToInt32(gvStatsFR.Rows[i].Cells[2].Text);
            }
        }
        TotalAtlasKbOut = TotalAtlasKbOut + TotalFR_Mb*1024;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        lbHeader.Text = "с " + cldrDateStart.SelectedDate.Date.AddDays(-1).ToShortDateString() + " по " + cldrDateStop.SelectedDate.Date.ToShortDateString();            
        //gvStatsEP.Columns[6].Visible = false;
        //gvStatsEP_SNG.Columns[6].Visible = false; 
        //gvIstok.Columns[4].Visible = false;
        //Page.StyleSheetTheme = "print";
        //Response.Redirect("Stats.aspx");
        /*
        Response.Redirect("StatsReport.aspx?lab1="+lbStatsEP.Text+
            "&" + "lab2=" + lbStatsEP_SNG.Text+
            "&" + "lab3=" + lbStatsHosts.Text+
            "&" + "lab4=" + lbHeader.Text);
        */
    }
    protected void rblStatType_SelectedIndexChanged(object sender, EventArgs e)
    {
        mvStats.ActiveViewIndex = rblStatType.SelectedIndex;
        cldrDateStart_SelectionChanged(sender, e);
    }
}
